/*
*** Heterogeneity analysis to explore potential mechanisms
	// input: filledpanel, union_category, union_boards_panel_20112019
	// output: Table4.xlsx (sheet: t4_panelA)
*/

cap log close
cap log using "$logs/amenities_heterogx_log", replace
	
	
*****************
** CBA CLAUSES **
*****************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel

	* Use contract data
	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	merge m:1 union_id using "$raw\CNES\union_category"
	drop if _merge==2
	drop _merge
	
	preserve
		use "$files/union_boards_panel_20112019.dta", clear
		keep if year>=2012 & year<=2017
		gen xxx = sh_female if year==2014
		gen yyy = sh_female if year>2014
		egen xxxm = max(xxx), by(union_id)
		egen yyym = max(yyy), by(union_id)
		gen gain_sh_female = (xxxm<yyym)
		drop xxx* yyy*
		gen xxx = sh_female_exec if year==2014
		gen yyy = sh_female_exec if year>2014
		egen xxxm = max(xxx), by(union_id)
		egen yyym = max(yyy), by(union_id)
		gen gain_sh_female_exec = (xxxm<yyym)
		drop xxx* yyy*
		gen xxx = sh_female_execd if year==2014
		gen yyy = sh_female_execd if year>2014
		egen xxxm = max(xxx), by(union_id)
		egen yyym = max(yyy), by(union_id)
		gen gain_sh_female_execd = (xxxm<yyym)
		drop xxx* yyy*
		keep union_id gain_*
		duplicates drop
		tempfile temp
		save `temp'
	restore
	merge m:1 union_id using `temp'
	drop if _merge==2
	drop _merge
	
	gen d1_het = 1
	replace d1_het = 0 if ///
		(c_group=="servidores_publicos") | ///
		(c_group=="rural") | ///
		(c_subgroup=="ensino") | ///
		(c_subgroup=="fia_tec") | ///
		(c_group=="emp_credito") | ///
		(c_subgroup=="saude") | ///
		(c_subgroup=="comunica") | ///
		(c_subgroup=="prof_lib")
	
	gen d2_het = 1
	replace d2_het = 0 if ///
		(c_subgroup=="metalurgica") | ///
		(c_subgroup=="saude") | ///
		(c_subgroup=="atacadista"|c_subgroup=="varejista"|c_subgroup=="autonomo") | ///
		(c_subgroup=="comunica")  
	
	gen d3_het = 1
	replace d3_het = 0 if ///
		(microregion_mode==35061)| ///
		(microregion_mode==26017)| ///
		(microregion_mode==42016)| ///
		(microregion_mode==52010)| ///
		(microregion_mode==31030)| ///
		(microregion_mode==13007)
		
	gen d4_het = 1
	replace d4_het = 0 if ///
		(gain_sh_female_execd==1)
		
	tab d1_het
	tab d2_het
	tab d3_het
	tab d4_het

	* time varying fixed effects
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}
	
	* for clustering
	egen clustergrp = group(`clstr')
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'

	
	***FEMALE***
	* Columns 1: Main results
	************************************************************

	local depvar female_clauses_DD 

	local j = 1
	foreach var of local depvar {	
			
			*** All signing firms
			reghdfe `var' treatpost , absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			estadd scalar sumdum = 0
			estadd scalar sumdump = 0
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store a1`j'
												
			local ++j
				
			}
	
	* Column 2 : interact with industries gaining female representative in CUT (union info)
	************************************************************

	local depvar female_clauses_DD 
	gen dummy = (d1_het==0) if d1_het!=.
	
	local j = 1
	foreach var of local depvar {	
			
			*** All signing firms
			reghdfe `var' treatpost##dummy treat##dummy post##dummy, absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom 1.treatpost + 1.treatpost#1.dummy
			estadd scalar sumdum =r(estimate)
			qui test 1.treatpost + 1.treatpost#1.dummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store b1`j'
												
			local ++j
				
			}
	drop dummy	
	
	
	* Column 3 : interact with industries proposing fight plan (union info)
	************************************************************

	local depvar female_clauses_DD 
	gen dummy = (d2_het==0) if d2_het!=.
	
	local j = 1
	foreach var of local depvar {	
			
			*** All signing firms
			reghdfe `var' treatpost##dummy treat##dummy post##dummy, absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom 1.treatpost + 1.treatpost#1.dummy
			estadd scalar sumdum =r(estimate)
			qui test 1.treatpost + 1.treatpost#1.dummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store c1`j'
												
			local ++j
				
			}
	drop dummy	
		
		
	* Columns 4: interact with microregions with CUT schools 
	*********************************************************************
	local depvar female_clauses_DD 
	gen dummy = (d3_het==0) if d3_het!=.
	
	local j = 1
	foreach var of local depvar {		
			
			*** All signing firms
			reghdfe `var' treat treatpost##dummy treat##dummy post##dummy, absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom 1.treatpost + 1.treatpost#1.dummy
			estadd scalar sumdum =r(estimate)
			qui test 1.treatpost + 1.treatpost#1.dummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store d1`j'
												
			local ++j
				
			}
	drop dummy
		
	
	* Columns 5: gaining leader interaction 
	*********************************************************************
	local depvar female_clauses_DD
	gen dummy = (d4_het==0) if d4_het!=.
	gen treatdummy = treat*dummy
	gen treatpostdummy = treatpost*dummy
	
	local j = 1
	foreach var of local depvar {		
			
			*** All signing firms
			reghdfe `var' treat post treatpost treatdummy treatpostdummy, absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom treatpost + treatpostdummy
			estadd scalar sumdum =r(estimate)
			qui test treatpost + treatpostdummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store e1`j'
												
			local ++j
				
			}
	drop dummy treatdummy treatpostdummy
	
			
	*** Export ***
	forvalues j = 1/1 {
			#d ;
			estout a1`j' b1`j' c1`j' d1`j' e1`j' using "$tables/T4_xhet_`j'.txt", 
			style(tab) mlabels(none) label collabels(none)
			cells(b(star fmt(%9.3f)) se(par)) 
			stats(sumdum sumdump meandv obs, fmt(%9.3f %9.3f %9.2fc %9.0fc ) labels("sum" "p-value" "mean depvar" "N" )) 
			drop(o.*, relax) keep(treatpost 1.treatpost 1.treatpost#1.dummy treatpostdummy, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
			#d cr
		}	
				
	local lab1 Intensive_margin
	
	local r1 = 1
	
	import delimited "$tables/T4_xhet_1.txt", clear
	rename v1 `lab1'
	rename v2 main
	rename v3 new_reps
	rename v4 conf_prop
	rename v5 cut_school
	rename v6 fem_leader_int
			
	export excel using "$tables/Table4.xlsx", sheet("t4_panelA") cell(A`r1') firstrow(var) sheetreplace

cap log close
